package com.topone.config;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2017/11/20,0020.
 */
@Component
@Slf4j
public class WechatMpConfig {
    @Autowired
    private WechatAccountConfig accountConfig;


    @Bean
    @ConditionalOnMissingBean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(accountConfig.getMpAppId());
        wxMpConfigStorage.setSecret(accountConfig.getMpAppSecret());
        wxMpConfigStorage.setToken(accountConfig.getToken());
        wxMpConfigStorage.setAesKey(accountConfig.getAesKey());
        wxMpConfigStorage.setNotifyURL(accountConfig.getNotifyUrl());
        wxMpConfigStorage.setPartnerId(accountConfig.getMchId());
        wxMpConfigStorage.setPartnerKey(accountConfig.getMchKey());
        wxMpConfigStorage.setTradeType("JSAPI");

        return wxMpConfigStorage;
    }

    @Bean
    @ConditionalOnMissingBean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

}